package com.xianbeiying.app.ckgl.fragment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.xianbeiying.app.ckgl.R;
import com.xianbeiying.app.ckgl.adapter.GridSelectImageAdapter;
import com.xianbeiying.app.ckgl.bean.EsSubscBean;
import com.xianbeiying.app.ckgl.http.HttpCallBackUtil;
import com.xianbeiying.app.ckgl.model.SetModel;
import com.xianbeiying.app.ckgl.photo.activity.PhotoPagerActivity;
import com.xianbeiying.app.ckgl.photo.activity.PhotoPickerActivity;
import com.xianbeiying.app.ckgl.photo.util.PhotoPickerIntent;
import com.xianbeiying.app.ckgl.util.BitmapAndStringUtils;
import com.xianbeiying.app.ckgl.util.LogUtils;
import com.xianbeiying.app.ckgl.util.PhoneUtil;
import com.xianbeiying.app.ckgl.util.StringUtil;
import com.xianbeiying.app.ckgl.util.ToastUtil;
import com.xianbeiying.app.ckgl.widght.MyGridView;

import java.util.ArrayList;
import java.util.List;

/**
 * 远程诊断
 */
public class EsDiagnoseFragment extends BaseFragment  implements HttpCallBackUtil{
   private View view;
    private TextView tv_wtsc;
    private ImageView iv_add_scimg;
    private MyGridView scimg_grid;
    private GridSelectImageAdapter gridSelectImageAdapter;
    private EditText et_esyc_name,et_esyc_phone;
    private EditText et_trouble_content;
    private Button es_yczd_btn;
    private ScrollView sc_yczd;
    private static EsDiagnoseFragment esdFragment;
    public static EsDiagnoseFragment getInstance(){
        esdFragment=new EsDiagnoseFragment();
        return esdFragment;
    }
    private SetModel setModel;
    private static final int ADD_CHOOSE = 0;
    private static final int MODIFY_CHOOSE = 1;
    private static final int MAX_PIC_NUM = 3;
    private ArrayList<String> imgPaths = new ArrayList<>();
    private boolean isMulti;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view=inflater.inflate(R.layout.fragment_es_yczd,null);
        initView();
        return view;
    }
    private ArrayList<String> imgUrl=new ArrayList<>();
    private void initView() {
        setModel=new SetModel(getActivity());
        setModel.addResponseListener(this);
        sc_yczd=(ScrollView) view.findViewById(R.id.sc_yczd);
        sc_yczd.smoothScrollBy(0,0);
        tv_wtsc=(TextView) view.findViewById(R.id.tv_wtsc);
        iv_add_scimg=(ImageView) view.findViewById(R.id.iv_add_scimg);
        scimg_grid=(MyGridView) view.findViewById(R.id.scimg_grid);
        et_esyc_name=(EditText) view.findViewById(R.id.et_esyc_name);
        et_esyc_phone=(EditText) view.findViewById(R.id.et_esyc_phone);
        es_yczd_btn=(Button) view.findViewById(R.id.es_yczd_btn);
        imgUrl.clear();
        et_trouble_content=(EditText) view.findViewById(R.id.et_trouble_content);
        es_yczd_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String trouble=et_trouble_content.getText().toString().trim();
                String esycName=et_esyc_name.getText().toString().trim();
                String esycPhone=et_esyc_phone.getText().toString().trim();
                if(trouble.isEmpty()){
                    ToastUtil.showInfo(getActivity(),"描述不能为空");
                    return;
                }
                if(esycName.isEmpty()){
                    ToastUtil.showInfo(getActivity(),"姓名不能为空");
                    return;
                }
                if(imgPaths.size()==0){
                    ToastUtil.showInfo(getActivity(),"请添加图片");
                    return;
                }
                if(esycPhone.isEmpty()){
                    ToastUtil.showInfo(getActivity(),"手机号不能为空");
                    return;
                }
                if (!PhoneUtil.isPhone(esycPhone)){
                    ToastUtil.showInfo(getActivity(),"手机号格式不正确");
                    return;
                }
                imgUrl.clear();
                for(int i=0;i<imgPaths.size();i++){
                    Bitmap bitmap = StringUtil.getLoacalBitmap(imgPaths.get(i));
                    if(imgPaths.get(i).contains(".png")){
                        imgUrl.add("png;"+BitmapAndStringUtils.convertIconToString(bitmap));
                    }
                    if(imgPaths.get(i).contains(".jpg")){
                        imgUrl.add("jpg;"+BitmapAndStringUtils.convertIconToString(bitmap));
                    }
                }
               // 远程诊断
                setModel.ADDDiagnose("EXPERT_DIAGNOSE",trouble,esycName,esycPhone,imgUrl,true);
            }
        });
        initData();
    }
    private void initData() {
        gridSelectImageAdapter = new GridSelectImageAdapter(getActivity(), imgPaths, MAX_PIC_NUM);
        //加载数据
        scimg_grid.setAdapter(gridSelectImageAdapter);
        scimg_grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (position < imgPaths.size()) {
                    Intent intent = new Intent(getActivity(), PhotoPagerActivity.class);
                    intent.putExtra(PhotoPagerActivity.EXTRA_CURRENT_ITEM, position);
                    intent.putExtra(PhotoPagerActivity.EXTRA_PHOTOS, imgPaths);
                    startActivityForResult(intent, MODIFY_CHOOSE);
                } else {
                    //Add Photo
                    PhotoPickerIntent intent = new PhotoPickerIntent(getActivity());
                    intent.setPhotoCount(MAX_PIC_NUM - imgPaths.size());
                    intent.setMultiChoose(isMulti);
                    startActivityForResult(intent, ADD_CHOOSE);
                }
            }
        });
        iv_add_scimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtils.e("dddd","=====");
                PhotoPickerIntent intent = new PhotoPickerIntent(getActivity());
                intent.setPhotoCount(MAX_PIC_NUM - imgPaths.size());
                intent.setMultiChoose(isMulti);
                startActivityForResult(intent, ADD_CHOOSE);
            }
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<String> photos = null;
        if (resultCode == getActivity().RESULT_OK && requestCode == MODIFY_CHOOSE) {
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
            }
            imgPaths.clear();
            if (photos != null) {
                imgPaths.addAll(photos);
            }
            gridSelectImageAdapter.notifyDataSetChanged();
        } else if (resultCode==getActivity(). RESULT_OK && requestCode == ADD_CHOOSE) {
            if (data != null) {
                photos = data.getStringArrayListExtra(PhotoPickerActivity.KEY_SELECTED_PHOTOS);
            }
            if (photos != null) {
                imgPaths.addAll(photos);
            }
            gridSelectImageAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onCallBackData(String url,Object content) {
        if(url.equals("远程诊断")){
            if(content!=null){
                    EsSubscBean esSubscBean= JSON.parseObject(new String((byte[]) content), EsSubscBean.class);
                    if(esSubscBean.isR()){
                        ToastUtil.showInfo(getActivity(),"提交成功");
                        et_trouble_content.setText("");
                        et_esyc_name.setText("");
                        et_esyc_phone.setText("");
                        imgPaths.clear();
                        gridSelectImageAdapter.notifyDataSetChanged();
                        sc_yczd.smoothScrollBy(0,0);

                    }else{
                        ToastUtil.showInfo(getActivity(),esSubscBean.getERR_MSG());

                    }
            }

        }

    }
}
